<?php

namespace App\Admin\Controllers;

use App\crm_customer;
use App\Admin_users;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Chart\Radar;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\Tools\Duanxin;
use App\Admin\Extensions\Tools\DuanxinGender;

class Crm_customerController extends Controller
{
    use ModelForm;

    
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('客户管理');
            $content->description('客户列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('客户管理');
            $content->description('客户详情');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_customer::class, function (Grid $grid) {
            if(Admin::user()->isAdministrator()){
                
            }else{
                $grid->model()->where('customer_weihu',Admin::user()->id);
            }
            $grid->id('序号')->sortable();
            $grid->customer_name('客户姓名');
            $grid->customer_phone('客户电话');
            $grid->customer_type('客户类别');
            $grid->customer_danwei('单位');
            $list=['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他'];
            $grid->column('customer_laiyuan','客户来源')->display(function ($customer_laiyuan) use ($list) {
                return $list[$customer_laiyuan];
            });
            $grid->column('admin_users.username','维护人');
            $grid->column('id','生日提醒')->display(function ($id) {
                if(DB::table("crm_shengri")::where("customer_id",$id)->where('date',date("Y"))->get()){
                    return "已发送";
                }else{
                    return "未发送";
                }
            });
            $grid->customer_kfdate('录入时间');
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                $filter->equal('customer_type','客户类别')->select(['单位'=>'单位','个人'=>'个人']);
                $filter->equal('customer_laiyuan','客户来源')->select(['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他']);
                $filter->like('customer_name','客户姓名');
                $filter->equal('customer_phone','客户电话');
                $filter->like('customer_danwei','单位名称');
                if(Admin::user()->roles[0]->name=="总经理"){
                    $filter->like('customer_weihu','维护人');
                }
            });
            $grid->tools(function ($tools) {
                $tools->append(new DuanxinGender());//调用隐藏的弹出层按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();//禁用批量删除
                    $batch->add('短信发送', new Duanxin());
                });
            });
            $grid->disableExport();//禁用导出数据按钮
            $grid->disableCreation();       //把新增框干掉
            $script_addones=<<<EOT
                $("#duanxin").css("display","none");

EOT;
                Admin::script($script_addones); 
        });
    }

    
    protected function form()
    {
        return Admin::form(crm_customer::class, function (Form $form) {
            $form->display('id', '序号');
            $form->select('customer_laiyuan','客户来源')->options(['1'=>'网站','2'=>'转介绍','3'=>'上门','4'=>'电话销售','5'=>'老客户','6'=>'其他']);
            $form->text('customer_name', '客户姓名')->rules('required');
            $form->text('customer_age', '客户年龄')->rules('required');
            $form->select('customer_sex', '性别')->options(['1'=>'男','2'=>'女']);
            $form->mobile('customer_phone', '客户电话')->options(['mask' => '999 9999 9999']);
            $form->text('customer_weixin', '微信');
            $form->text('customer_qq', 'QQ');
            $form->email('customer_email','邮箱Email');
            $form->text('customer_sfz','身份证号码');
            $form->select('customer_type','客户类别')->options(['单位'=>'单位','个人'=>'个人']);
            $form->text('customer_danwei', '单位')->rules('required');
            $directors=Admin_users::all()->pluck('username','username');
            $form->select('customer_weihu', '维护人')->options($directors)->attribute(['disabled' => true]);
            $form->hidden('customer_kfdate','开发时间');
            
        });
    }
}
